﻿using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Services;
using Frogism.Core;
using Frogism.Domain;
using System.Text;
namespace Frogism.Web {
  /// <summary>
  /// Summary description for MoneyService
  /// </summary>
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  [ToolboxItem(false)]
  // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
  [System.Web.Script.Services.ScriptService]
  public class MoneyService : System.Web.Services.WebService {

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloWorld() {
      return "Hello My World";
    }
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string AccountDetails(int id) {
      MoneyManager am = new MoneyManager();
      IAccount account = am.Get(id);

      System.Text.StringBuilder builder = new System.Text.StringBuilder();
      builder.Append("<table class='AccountPopUp'>");
      //builder.Append("<div>" + account.Name + "</div>");
      builder.Append(StartRow(true));
      builder.Append(StartCell);


      Populate(builder, "Unconfirmed", MoneyManager.FilterUnconfirmed(account));

      builder.Append(EndCell);
      builder.Append(StartCell);

      Populate(builder, "Recent", MoneyManager.FilterRecent(account));

      builder.Append(EndCell);

      builder.Append(EndRow);
      builder.Append("</table>");
      return builder.ToString();
    }
    private void Populate(StringBuilder builder,string caption, Collection<ITransaction> transactions) {
      if(transactions.Count > 0) {
        builder.Append("<div>" + caption + "</div>");
        builder.Append("<table>");
        if(transactions.Count >= 20) {
          for(int i = 0;i < 20;i++) {
            ITransaction t = transactions[i];
            AddTransaction(builder, t);
          }
        } else {
          foreach(Transaction t in transactions) {
            AddTransaction(builder, t);
          }
        }
        builder.Append("</table>");
      } else {
        builder.Append("<div style='font-size:6pts'>(No " + caption + ")</div>");
      }
    }
    private void AddTransaction(System.Text.StringBuilder builder, ITransaction t) {
      builder.Append(StartRow(t.IsConfirmed));
      builder.Append(StartCell);
      builder.Append(t.TransactionDate.ToString("yyyy.MM.dd"));
      builder.Append(EndCell + StartCell);
      builder.Append(t.TransactionType.ToShortString());
      builder.Append(EndCell + StartCell);
      builder.Append(t.Amount.ToString());
      builder.Append(EndCell + StartCell);
      builder.Append(t.Note);
      builder.Append(EndCell);
      builder.Append(EndRow);

    }
    private string StartRow(bool isConfirmed) {
      if(!isConfirmed) {
        return "<tr valign='top' style='color:red'>";
      } else {
        return "<tr valign='top'>";
      }
    }
    private string EndRow {
      get { return "</tr>"; }
    }
    private string StartCell {
      get { return "<td nowrap='true'>"; }
    }
    private string EndCell {
      get { return "</td>"; }
    }
  }
}
